package android_rteditor.customsortricheditor.editor;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;

/**
 * 富文本编辑框
 * Created by chenxunlin01 on 2017/8/16.
 */

public class RichEditText extends android.support.v7.widget.AppCompatEditText implements ITextEditorItem {

    private OnEditorFocusChangeListener editorFocusChangeListener;
    private RichEditorController controller;

    public RichEditText(Context context, RichEditorController controller, OnEditorFocusChangeListener
            editorFocusChangeListener) {
        super(context);
        this.editorFocusChangeListener = editorFocusChangeListener;
        this.controller = controller;
        init();
    }

    private void init() {
        setPadding(0, 0, 0, 0);
        setLineSpacing(8, 1);
        setEditorBackgroundColor(controller.getEditTextBackgroundColor());
        setEditorTextColor(controller.getEditTextColor());
        setEditorTextSize(controller.getEditTextSize());
        setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                editorFocusChangeListener.onFocusChange(RichEditText.this, hasFocus);
            }
        });
        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
                    onBackspacePress();
                }
                return false;
            }
        });
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public int getType() {
        return EditorItemFactory.EDITOR_ITEM_EDITTEXT;
    }

    /**
     * 文本内容是否为空
     *
     * @return
     */
    @Override
    public boolean isEmptyText() {
        return TextUtils.isEmpty(getText().toString());
    }

    // 根据index获取焦点
    public void focus(boolean toEnd) {
        String text = getText().toString();
        if (toEnd) {
            focusByStartEnd(text.length(), text.length());
        } else {
            focusByStartEnd(0, 0);
        }
    }

    // 根据index获取焦点
    @Override
    public void focusByStartEnd(int start, int end) {
        requestEditorFocus();
        String text = getText().toString();
        if (start < 0 || start > text.length()) {
            start = 0;
        }
        if (end < 0 || end > text.length()) {
            end = text.length();
        }
        setSelection(start, end);
    }

    /**
     * 获取光标位置
     *
     * @return
     */
    @Override
    public int[] getSelectionStartEnd() {
        int[] startEnd = new int[2];
        startEnd[0] = getSelectionStart();
        startEnd[1] = getSelectionEnd();
        return startEnd;
    }

    // 防止已经有焦点时调用requestFocus，不会触发事件
    @Override
    public void requestEditorFocus() {
        requestFocus();
        editorFocusChangeListener.onFocusChange(this, true);
    }

    @Override
    public String getEditorText() {
        return getText().toString();
    }

    @Override
    public void setEditorText(String text) {
        setText(text);
    }

    @Override
    public void setEditorTextSize(float size) {
        setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    @Override
    public void setEditorTextColor(int colorRes) {
        setTextColor(colorRes);
    }

    @Override
    public void setEditorBackgroundColor(int colorRes) {
        setBackgroundColor(colorRes);
    }

    private void onBackspacePress() {
        int startSelection = getSelectionStart();
        // 只有在光标已经顶到文本输入框的最前方，在判定是否删除之前的图片，或两个View合并
        if (startSelection == 0) {
            int index = controller.indexOfItem(this);
            // 获取前一个视图
            IEditorItem preView = controller.getChildAt(index - 1);
            if (preView != null) {
                if (preView instanceof RichImage || preView instanceof RichAudio) {
                    controller.removeItem(preView);
                } else if (preView instanceof RichEditText) {
                    // 合并两个editext
                    RichEditText textEditorItem = (RichEditText) preView;
                    String curText = getEditorText().toString();
                    String preText = textEditorItem.getEditorText();
                    // 合并文本view时，不需要transition动画
                    controller.removeLayoutTransition();
                    controller.removeItem(RichEditText.this);
                    controller.recoveryLayoutTransition();
                    // 文本合并
                    textEditorItem.setEditorText(preText + curText);
                    // 设置光标
                    textEditorItem.focusByStartEnd(preText.length(), preText.length());
                } else if (preView instanceof CheckboxView) {
                    CheckboxView checkboxView = (CheckboxView) preView;
                    checkboxView.focus(true);
                }
            }
        }
    }
}
